package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsRecoveryVehicleBo;
import com.kxmall.huishou.domain.vo.HsRecoveryVehicleVo;
import com.kxmall.huishou.domain.HsRecoveryVehicle;
import com.kxmall.huishou.mapper.HsRecoveryVehicleMapper;
import com.kxmall.huishou.service.IHsRecoveryVehicleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 回收车辆信息Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-24
 */
@RequiredArgsConstructor
@Service
public class HsRecoveryVehicleServiceImpl implements IHsRecoveryVehicleService {

    private final HsRecoveryVehicleMapper baseMapper;

    /**
     * 查询回收车辆信息
     */
    @Override
    public HsRecoveryVehicleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询回收车辆信息列表
     */
    @Override
    public TableDataInfo<HsRecoveryVehicleVo> queryPageList(HsRecoveryVehicleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsRecoveryVehicle> lqw = buildQueryWrapper(bo);
        Page<HsRecoveryVehicleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询回收车辆信息列表
     */
    @Override
    public List<HsRecoveryVehicleVo> queryList(HsRecoveryVehicleBo bo) {
        LambdaQueryWrapper<HsRecoveryVehicle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsRecoveryVehicle> buildQueryWrapper(HsRecoveryVehicleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsRecoveryVehicle> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getVehicleNo()), HsRecoveryVehicle::getVehicleNo, bo.getVehicleNo());
        lqw.like(StringUtils.isNotBlank(bo.getName()), HsRecoveryVehicle::getName, bo.getName());
        lqw.eq(bo.getRiderId() != null, HsRecoveryVehicle::getRiderId, bo.getRiderId());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNum()), HsRecoveryVehicle::getPlateNum, bo.getPlateNum());
        lqw.eq(StringUtils.isNotBlank(bo.getVinNo()), HsRecoveryVehicle::getVinNo, bo.getVinNo());
        lqw.eq(bo.getLongitude() != null, HsRecoveryVehicle::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLatitude() != null, HsRecoveryVehicle::getLatitude, bo.getLatitude());
        lqw.eq(bo.getStatus() != null, HsRecoveryVehicle::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增回收车辆信息
     */
    @Override
    public Boolean insertByBo(HsRecoveryVehicleBo bo) {
        HsRecoveryVehicle add = BeanUtil.toBean(bo, HsRecoveryVehicle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改回收车辆信息
     */
    @Override
    public Boolean updateByBo(HsRecoveryVehicleBo bo) {
        HsRecoveryVehicle update = BeanUtil.toBean(bo, HsRecoveryVehicle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsRecoveryVehicle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除回收车辆信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
